class <%= class_name %>Controller < ApplicationController
  getter <%= @name %> = <%= class_name %>.new

  before_action do
    only [:show, :edit, :update, :destroy] { set_<%= @name %> }
  end

  def show
    render("show.<%= config.language %>")
  end

  def new
    render "new.<%= config.language %>"
  end

  def edit
    render("edit.<%= config.language %>")
  end

  def create
    <%= @name %> = <%= class_name %>.new <%= @name %>_params.validate!
    pass = <%= @name %>_params.validate!["password"]
    <%= @name %>.password = pass if pass

    if <%= @name %>.save
      session[:<%= @name %>_id] = <%= @name %>.id
      redirect_to "/", flash: {"success" => "Created <%= class_name %> successfully."}
    else
      flash[:danger] = "Could not create <%= class_name %>!"
      render "new.<%= config.language %>"
    end
  end

  def update
    <%= @name %>.set_attributes <%= @name %>_params.validate!
    if <%= @name %>.save
      redirect_to "/", flash: {"success" => "<%= class_name %> has been updated."}
    else
      flash[:danger] = "Could not update <%= class_name %>!"
      render "edit.<%= config.language %>"
    end
  end

  def destroy
    <%= @name %>.destroy
    redirect_to "/", flash: {"success" => "<%= class_name %> has been deleted."}
  end

  private def <%= @name %>_params
    params.validation do
      required :email
      optional :password
    end
  end

  private def set_<%= @name %>
    @<%= @name %> = current_<%= @name %>.not_nil!
  end
end
